package com.gaoxin.api.contronller;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.gaoxin.api.dao.common.Digests;
import com.gaoxin.api.dao.common.Encodes;
import com.gaoxin.api.entity.statisentity.StudentInfo;
import com.gaoxin.api.entity.statisentity.User;
import com.gaoxin.api.service.common.LoginService;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 登录接口
 * @author zhaozhenshe
 *2018-07-16
 */
@RestController
@RequestMapping("app/common")
@Api(tags="登录接口")
public class ApiLoginController {
	@Autowired
	private LoginService loginService;
    @ApiOperation("退出")
    @RequestMapping(value="/login",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String logout(@RequestBody Map<String,String> paramMap){
    	String userCode = paramMap.get("userCode");
    	String password = paramMap.get("password");
    	String jsonData = null;
    	User user = loginService.queryUserCode(userCode);
    	if(user==null){
    		Map<String, Object> map = Maps.newLinkedHashMap();
    		map.put("code","100002");
			map.put("errmsg","用户不存在!");
			jsonData = JSON.toJSONString(map);
			return jsonData;
    	}
    	try {
			Map<String, Object> map = Maps.newLinkedHashMap();
			//将密码加密
			String password2 = ApiLoginController.entryptPassword(user,password);
			User status = loginService.login(userCode, password2);
			if(status!=null){
				map.put("code","0");
				map.put("results","");
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code","100001");
				map.put("errmsg","用户名或密码错误!");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonData;
    }
    /**
	 * 密码加密
	 * @param plainPassword
	 * @return
	 */
	public static String entryptPassword(User user,String plainPassword) {  
		byte[] salt = Encodes.decodeHex(user.getPassword().substring(0, 16));
		byte[] hashPassword =  Digests.sha1(plainPassword.getBytes(), salt, 1024);   
		return  Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword);
	}
	/**
	 * 根据学生电话查询学生手机号
	 * @return
	 * 
	 */
	@RequestMapping(value="/queryStuNum",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryStuNumber(@RequestBody Map<String,String> paramMap){
		String phone = paramMap.get("phone");
		String jsonData = null;
		try {
			Map<String, Object> map = Maps.newLinkedHashMap();
			StudentInfo stu = loginService.queryStuNumber(phone);
			if(stu!=null){
				map.put("code","0");
				map.put("results",stu);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code","100003");
				map.put("errmsg","无该学生信息!");
				jsonData = JSON.toJSONString(map);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	@RequestMapping(value="/queryUserCode",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String queryUserCode(@RequestBody Map<String,String> paramMap){
		String mobile = paramMap.get("mobile");
		String jsonData = null;
		try {
			Map<String, Object> map = Maps.newLinkedHashMap();
			User user = loginService.queryUserInfo(mobile);
			if(user!=null){
				map.put("code","0");
				map.put("results",user);
				jsonData = JSON.toJSONString(map);
			}else{
				map.put("code","100004");
				map.put("errmsg","无该用户信息!");
				jsonData = JSON.toJSONString(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonData;
	}
	/**
	  * 查询用户账号
	  * @param paramMap
	  * @return
	  */
	 @RequestMapping(value="/queryUserId",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	 public String queryUserId(@RequestBody Map<String,String> paramMap){
	  String idCard = paramMap.get("idCard");
	  String jsonData = null;
	  try {
	   Map<String, Object> map = Maps.newLinkedHashMap();
	   User user = loginService.queryUserId(idCard);
	   if(user!=null){
	     map.put("code","0");
	     map.put("results",user);
	     jsonData = JSON.toJSONString(map);
	   }else{
	    map.put("code","100005");
	    map.put("errmsg","无该用户信息!");
	    jsonData = JSON.toJSONString(map);
	   }
	  } catch (Exception e) {
	   e.printStackTrace();
	  }
	  return jsonData;
	 }
	/**
	  * 查询所有的员工和学生信息
	  * @param paramMap
	  * @return
	  */
	 @RequestMapping(value="/queryAllUser",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	 public String findAllUser(){
	  String jsonData = null;
	  try {
	   Map<String, Object> map = Maps.newLinkedHashMap();
	   List<User> user = loginService.queryAllUser();
	   if(user!=null){
	        map.put("code","0");
	        map.put("results",user);
	        jsonData = JSON.toJSONString(map);
	   }else{
	       map.put("code","100006");
	       map.put("errmsg","无信息!");
	       jsonData = JSON.toJSONString(map);
	   }
	  } catch (Exception e) {
	   e.printStackTrace();
	  }
	  return jsonData;
	 }
}
